const router = require('koa-router')()
const models = require('../models')
const moment = require('moment')
const Sequelize = require('sequelize');
const Op = Sequelize.Op;

router.post('/classify_list', async ctx => {
    // 新闻分类列表
    let page = ctx.request.body.page
    let pageshow = ctx.request.body.pageshow
    let classify = await models.classify.findAndCountAll({
        order: [
            ['cy_id', 'DESC']
        ],
        limit: pageshow,
        offset: (page - 1)*pageshow
    })

    ctx.body = {
        code: 200,
        data: {
            list: classify.rows.map(item => {
                let obj = JSON.parse(JSON.stringify(item))
                return {
                    ...obj,
                    created_at: moment(obj.created_at*1000).format('YYYY-MM-DD HH:mm:ss')
                }
            }),
            count: classify.count
        },
        msg: '操作成功'
    }
})

router.post('/handle_classify', async ctx => {
    // 操作新闻分类
    let action = ctx.request.body.action
    let cy_name = ctx.request.body.cy_name
    let cy_id = ctx.request.body.cy_id
    if (action == 'add') {
        await models.classify.create({
            cy_name,
            cy_num: 0,
            created_at: Date.parse(new Date())/1000
        })
        return ctx.body = {
            code: 200,
            data: {},
            msg: '操作成功'
        }
    }
    if (cy_id && cy_name) {
        await models.classify.update({
            cy_name,
        }, {
            where: {
                cy_id
            }
        })
        return ctx.body = {
            code: 200,
            data: {},
            msg: '操作成功'
        }
    }
    if (action == 'del') {
        await models.classify.destroy({
            where: {
                cy_id
            }
        })

        await models.news.destroy({
            where: {
                cy_id
            }
        })
        
        return ctx.body = {
            code: 200,
            data: {},
            msg: '删除成功'
        }
    }
})

router.post('/news_list', async ctx => {
    // 新闻列表
    let news_id = ctx.request.body.news_id
    let cy_id = ctx.request.body.cy_id
    let page = ctx.request.body.page
    let pageshow = ctx.request.body.pageshow
    if (news_id && cy_id) {
        // 删除文章
        let classify = await models.classify.findOne({
            where: {
                cy_id
            }
        })
        await classify.set('cy_num', classify.cy_num - 1).save()

        await models.news.destroy({
            where: {
                news_id
            }
        })
        return ctx.body = {
            code: 200,
            data: {},
            msg: '操作成功'
        }
    } else {

        let news = await models.news.findAndCountAll({
            order: [
                ['created_at', 'DESC']
            ],
            include: {
                model: models.classify,
                attributes: ['cy_name'],
            },
            limit: pageshow,
            offset: (page - 1)*pageshow
        })
    
        return ctx.body = {
            code: 200,
            data: {
                list: news.rows.map(item => {
                    let obj = JSON.parse(JSON.stringify(item))
                    return {
                        ...obj,
                        created_at: moment(obj.created_at*1000).format('YYYY-MM-DD HH:mm:ss'),
                    }
                }),
                count: news.count
            },
            msg: '操作成功'
        }
    }

})

router.post('/handle_news', async ctx => {
    // 操作新闻文章
    let news_id = ctx.request.body.news_id
    let news_title = ctx.request.body.news_title
    let news_cover = ctx.request.body.news_cover
    let cy_id = ctx.request.body.cy_id
    let introduce = ctx.request.body.introduce
    let article = ctx.request.body.article


    if (news_id) {
        // 更新新闻文章
        let news = await models.news.findOne({
            where: {
                news_id
            }
        })
        let oldclassify = await models.classify.findOne({
            where: {
                cy_id: news.get('cy_id')
            }
        })
        let newclassify = await models.classify.findOne({
            where: {
                cy_id
            }
        })
        if (cy_id == news.get('cy_id')) {
            
        } else {
            await oldclassify.set('cy_num', oldclassify.cy_num - 1).save()
            await newclassify.set('cy_num', newclassify.cy_num + 1).save()
        }

        await models.news.update({
            news_title,
            news_cover,
            cy_id,
            introduce,
            article
        },{
            where: {
                news_id
            }
        })
        return ctx.body = {
            code: 200,
            data: {},
            msg: '操作成功'
        }
    } else {
        //　添加新闻文章
        let classify = await models.classify.findOne({
            where: {
                cy_id
            }
        })
        await classify.set('cy_num', classify.cy_num + 1).save()

        await models.news.create({
            news_title,
            news_cover,
            cy_id,
            introduce,
            article,
            created_at: Date.parse(new Date())/1000
        })
        return ctx.body = {
            code: 200,
            data: {},
            msg: '操作成功'
        }
    }
})

module.exports = router